﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace FastShip88
{
    public partial class UserInfo : System.Web.UI.Page
    {
        System.Guid guidUser;
        DBAccess.UsersDataContext db ;

  
        protected void Page_Load(object sender, EventArgs e)
        {
            guidUser = (System.Guid)Membership.GetUser().ProviderUserKey;

            

            db = new DBAccess.UsersDataContext();

            if (!Page.IsPostBack)
            {
                init_page();

            }

        }

        private void init_page()
        {
            var q_userLevels = from ul in db.UserLevels select ul;

            ddlUserLevel.DataSource = q_userLevels;
            ddlUserLevel.DataValueField = "Id";
            ddlUserLevel.DataTextField = "Name";
            ddlUserLevel.DataBind();

            var users = db.UserInfos;
            var q_users = (from u in users
                     where u.UserId == guidUser
                     select u);

            if(q_users.Count() >0)
            {
                var q = q_users.First();
                txtLastName.Text = q.LastName.Trim();
                txtFirstName.Text = q.FirstName.Trim();
                
                txtPhone.Text = q.Phone == null ? "": q.Phone.Trim();
                txtMobile.Text = q.Mobile  == null ? "": q.Mobile.Trim();
                txtAddress.Text = q.Address == null ? "" : q.Address.Trim();
                txtPostCode.Text = q.PostCode == null ? "" : q.PostCode.Trim();
                txtEmail.Text = q.Email == null ? "" : q.Email.Trim();
                txtTaobao.Text = q.Taobao == null ? "" : q.Taobao.Trim();
                txtQQ.Text = q.QQ == null ? "" :  q.QQ.Trim();
                txtMSN.Text = q.MSN == null ? "" : q.MSN.Trim();
                txtMemo.Text = q.Memo == null ? "" : q.Memo.Trim();
                ddlUserLevel.SelectedValue = q.UserLevelId.ToString();

            }
            else
            {

                var membership = db.aspnet_Memberships;

                var q1 = (from m in membership
                     where m.UserId == guidUser
                     select m).First();


                txtEmail.Text = q1.Email;
            }


            
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var users = db.UserInfos;
            var q_users = (from u in users
                     where u.UserId == guidUser
                     select u);


            if (q_users.Count() > 0)
            {
                var q = q_users.First();
                q.LastName = txtLastName.Text.Trim();
                q.FirstName = txtFirstName.Text.Trim();
                q.Phone = txtPhone.Text.Trim();
                q.Mobile = txtMobile.Text.Trim();
                q.Address = txtAddress.Text.Trim();
                q.PostCode = txtPostCode.Text.Trim();
                q.Email = txtEmail.Text.Trim();
                q.Taobao = txtTaobao.Text.Trim();
                q.QQ = txtQQ.Text.Trim();
                q.MSN = txtMSN.Text.Trim();
                q.Memo = txtMemo.Text.Trim();
                q.UserLevelId = SafeConvert.ToInt(ddlUserLevel.SelectedValue);
                db.SubmitChanges();
            }
            else
            {
                var curUser = new DBAccess.UserInfo();
                curUser.UserId = guidUser;
                curUser.LastName = txtLastName.Text.Trim();
                curUser.FirstName = txtFirstName.Text.Trim();
                curUser.Phone = txtPhone.Text.Trim();
                curUser.Mobile = txtMobile.Text.Trim();
                curUser.Address = txtAddress.Text.Trim();
                curUser.PostCode = txtPostCode.Text.Trim();
                curUser.Email = txtEmail.Text.Trim();
                curUser.Taobao = txtTaobao.Text.Trim();
                curUser.QQ = txtQQ.Text.Trim();
                curUser.MSN = txtMSN.Text.Trim();
                curUser.Memo = txtMemo.Text.Trim();


                db.UserInfos.InsertOnSubmit(curUser);
                db.SubmitChanges();
            }
        }
    }
}
